<template>
  <div class="ssc-list org-person-list">
    <div class="content">
      <div class="ssc-search">
        <a-form autocomplete="off">
          <a-row :gutter="24">
            <a-col :span="6">
              <a-form-item label="策略名称：">
                <a-input v-model:value="searchParam.strategyName" placeholder="请输入" :maxlength="20" allowClear />
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-row justify="end">
                <a-button @click="clear()">重置</a-button>
                <a-button class="ml-4" type="primary" @click="search()">查询</a-button>
              </a-row>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="ssc-table">
        <div class="ssc-table-header">
          <span class="ssc-table-header-title">策略管理</span>
          <a-button type="primary" @click="add()">
            <template #icon><plus-outlined :style="{ 'font-weight': 'bold' }" /></template>
            添加策略
          </a-button>
        </div>
        <a-table :columns="table.columns" :data-source="dataList" :loading="loading" :pagination="false">
          <template #bodyCell="{ column, record }">
            <template v-if="column.key === 'policy'">{{ record.policy.id }}</template>
            <template v-if="column.key === 'policyName'">{{ record.privateProperties?.policyName || '--' }}</template>
            <template v-if="column.key === 'action'">
              <span>
                <a @click="edit(record)">编辑</a>
                <a-divider type="vertical" />
                <a-popconfirm placement="leftBottom" ok-text="确定" cancel-text="取消" @confirm="updateServiceState(record)">
                  <template #title>
                    <p>删除确认</p>
                    <p class="w-[200px]">您确认要将该策略删除吗？</p>
                  </template>
                  <a href="javascript:;">删除</a>
                </a-popconfirm>
              </span>
            </template>
          </template>
        </a-table>
        <SimplePagination v-model:currentPage="pagination.current" :lastResponseSize="pagination.currentSize" />
      </div>
    </div>
    <div class="modal">
      <Add v-model:visible="modalAdd.visible" @success="modalAdd.success" />
      <Edit v-model:visible="modalEdit.visible" :data="table.selectedRecord" @success="modalEdit.success" />
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { PlusCircleOutlined, PlusOutlined } from '@ant-design/icons-vue'
  import type { SelectProps } from 'ant-design-vue'
  import { message } from 'ant-design-vue'
  import type { Dayjs } from 'dayjs'
  import { onMounted, reactive, ref, watch } from 'vue'
  import { useRoute, useRouter } from 'vue-router'

  import { requestDeleteStrategy, requestStrategyList } from '/@/api/business/space'
  import { getApplyByPageEnum } from '/@/api/manage/openApiApp'
  import { HttpCodeEnum } from '/@/enums/httpEnum'
  import { CatalogTypeDesc, CatalogTypeList } from '/@/enums/spaceEnum'
  import { formatToDateTime } from '/@/utils/date'
  import SimplePagination from '/@/views/main/dataSpace/component/SimplePagination.vue'
  const router = useRouter()
  const route = useRoute()
  import Add from './components/Add.vue'
  import Edit from './components/Edit.vue'
  type RangeValue = [Dayjs, Dayjs]
  const timeRange = ref<RangeValue>()
  const variable = reactive({
    category: {},
  })

  // 服务类型列表
  const dataList = ref()

  const loading = ref(false)

  /** 查询条件 */
  const searchParam = reactive({
    catalogId: route.query.catalogId as string,
  })
  //驳回
  const modalData = reactive({
    visible: true,
    content: null,
    type: null,
  })

  /** 表格配置 */
  const table = reactive({
    columns: [
      { title: '策略ID', dataIndex: 'policy', key: 'policy' },
      { title: '策略名称', dataIndex: 'policyName', key: 'policyName' },
      { title: '更新时间', dataIndex: 'createdAt', customRender: ({ record }) => formatToDateTime(parseInt(record.createdAt)) },
      { title: '操作', key: 'action' },
    ],
    selectedRecord: {} as UserDeptListTypes.IResponse,
  })

  /**
   * 查询
   */
  const search = () => {
    queryList({ current: 1 })
  }

  const clear = () => {
    searchParam.strategyName = ''
  }

  // 分页配置
  const pagination = reactive({
    current: 1,
    currentSize: 10,
  })

  /**
   * 查询列表
   */
  const queryList = (params = {}) => {
    loading.value = true
    params.pageIndex = pagination.current
    params.strategyName = searchParam.strategyName
    requestStrategyList(params).then((res) => {
      loading.value = false
      if (res.responseCode === HttpCodeEnum.SUCCESS) {
        pagination.currentSize = res.responseData.list.length
        dataList.value = res.responseData.list
      }
    })
  }

  const panelChange = (value, mode) => {
    console.log(value, mode)
    searchParam.startTime = mode[0]
    searchParam.endTime = mode[1]
  }
  /** 新增弹窗 */
  const modalAdd = reactive({
    visible: false,
    success: () => queryList(),
  })
  /**
   * 新增
   */
  const add = () => {
    modalAdd.visible = true
    // router.push('/data-space/asset/interface')
  }

  /** 编辑弹窗 */
  const modalEdit = reactive({
    visible: false,
    success: () => queryList(),
  })

  /**
   * 编辑
   */
  const edit = (record) => {
    table.selectedRecord = record
    modalEdit.visible = true
  }
  const applyList = ref<any[]>([])
  /**
   * 获取订阅组列表
   */
  const queryRecordList = () => {
    search()
  }
  queryRecordList()

  watch(
    () => pagination.current,
    () => {
      queryList()
    },
  )
  const toAPIManage = (record) => {
    router.push({
      path: '/service-provider/serve/api',
      query: { serviceUuid: record.uuid },
    })
  }

  const updateServiceState = async (service) => {
    const { responseCode } = await requestDeleteStrategy({ repid: service.definitionId })
    if (responseCode === HttpCodeEnum.SUCCESS) {
      queryList()
      message.success(`操作成功`)
    }
  }
</script>

<style lang="less" scoped>
  .org-person-list {
    display: flex;
  }

  .org {
    width: 200px;
    height: 100%;
    padding: 18px;
    background: #fff;

    .org-title {
      margin-bottom: 12px;
      font-weight: bold;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .anticon-plus-circle {
        cursor: pointer;
        color: #08979c;
      }
    }
  }

  .content {
    flex: 1;
  }

  .ssc-search {
    margin-left: 16px;
    padding-bottom: 24px;
  }

  .ssc-table {
    margin-left: 16px;
    padding-bottom: 10px;

    .dept-leader {
      position: relative;
      top: -2px;
      height: 20px;
      margin-right: 4px;
      padding: 0 8px;
      border: none;
      font-size: 10px;
      background: #87d068;
    }
  }
</style>
